package com.xxxx.portal.interceptor;

import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author Lionel
 * @Date 2020/11/6 13:01
 * Created by IntelliJ IDEA 2020.1.
 * Project: shop
 * Description：全局拦截器
 */
@Component
public class CommonInterceptor implements HandlerInterceptor {

    @Value("${shop.order.url}")
    private String orderUrl;

    /**
     * 获取购物车跳转预订单时所需的URL
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取上下文对象
        ServletContext servletContext = request.getSession().getServletContext();
        //判断查询作用域中是否有已存的url
        String orderUrl = (String) servletContext.getAttribute("orderUrl");
        if (StringUtils.isEmpty(orderUrl)){
            //如果为空，则获取url放入作用域
            servletContext.setAttribute("orderUrl", this.orderUrl);
        }
        //放行
        return true;
    }
}
